const fs = require('fs')

function looseJsonParse(obj) {
  return Function('"use strict";return (' + obj + ')')()
}
let currentProxy = ''
function getUrl(key) {
  const router = fs.readFileSync('./proxy-config.js', 'utf8')
  const a = router.indexOf('{')
  const b = router.lastIndexOf('}')
  const proxy = looseJsonParse(router.substring(a, b + 1))
  if (currentProxy !== proxy[key]) {
    console.log(`${key} proxy changed =>`, proxy[key])
  }
  currentProxy = proxy[key]
  return proxy[key]
}
module.exports = {
  '/api': {
    target: 'target', // 这个字段必须有
    router: () => getUrl('api'),
    changeOrigin: true,
    pathRewrite: {
      '^/api': ''
    }
  },
  '/energy-carbon': {
    target: 'target',
    router: () => getUrl('message'),
    changeOrigin: true
    // pathRewrite: {
    //   '^/message': ''
    // }
  }
}
